function getRootPath(){  
    //获取当前网址，如： http://localhost:8088/test/test.jsp  
    var curPath=window.document.location.href;  
    //获取主机地址之后的目录，如： test/test.jsp  
    var pathName=window.document.location.pathname;  
    var pos=curPath.indexOf(pathName);  
    //获取主机地址，如： http://localhost:8088  
    var localhostPaht=curPath.substring(0,pos);  
    //获取带"/"的项目名，如：/test  
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
    return(localhostPaht+projectName);  
};
var path = getRootPath();

function isMobile(){
 var sUserAgent= navigator.userAgent.toLowerCase(),
 bIsIpad= sUserAgent.match(/ipad/i) == "ipad",
 bIsIphoneOs= sUserAgent.match(/iphone os/i) == "iphone os",
 bIsMidp= sUserAgent.match(/midp/i) == "midp",
 bIsUc7= sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4",
 bIsUc= sUserAgent.match(/ucweb/i) == "ucweb",
 bIsAndroid= sUserAgent.match(/android/i) == "android",
 bIsCE= sUserAgent.match(/windows ce/i) == "windows ce",
 bIsWM= sUserAgent.match(/windows mobile/i) == "windows mobile",
 bIsWebview = sUserAgent.match(/webview/i) == "webview";
 return (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM);
}



$(document).ready(function(){

    $(".doflag").on('click',function(){
        if($(".doflag").hasClass("up")){
            $(".doflag").addClass("down")
            $(".doflag").removeClass("up")
            $(".doflag").text("展开");
            $(".order").hide();
        }else if($(".doflag").hasClass("down")){
            $(".doflag").addClass("up")
            $(".doflag").removeClass("down")
            $(".doflag").text("收起");
            $(".order").show();
        }
    })
    $(".more").on('click',function(){
        if($(this).hasClass("more_show")){
            $(this).removeClass("more_show");
            $(".aline").addClass("aline_ac");
            $(this).text("更多");
        }else{
            $(this).addClass("more_show");
            $(".aline").removeClass("aline_ac");
            $(this).text("收起");
        }
    })



	if(isMobile()){
	$("#bankKind").on('click',function(){
		$("#xq").hide()
		$(".allBank").show()
	})
	
	
	$(".bankyh").on('click',function(){
		$(".allBank").hide()
		$("#xq").show()
	})
	}

});

function applyPay() {
	var bankCode = $("input[type='radio']:checked").val() || $("#select_bank_name").val();
	if( bankCode == '-1' || bankCode == ''){
		var bankCodeObj = $(".error_1:eq(1)");
        //bankCodeObj.html("请选择银行");
        //bankCodeObj.removeClass("none");
        //bankCodeObj.addClass("show");
        //errorShow(bankCodeObj);
        
        var bankCodeObj1 = $(".error_1:eq(0)");
        //bankCodeObj1.html("请选择银行");
        //bankCodeObj1.removeClass("none");
        //bankCodeObj1.addClass("show");
        //errorShow(bankCodeObj1);
        alert("请选择银行");
		return;
	}

	var cardNo = $("#cardNo").val();//银行卡号
	var authCode = $("#authCode").val();//验证码
	if(cardNo == ''){
        var cardNoObj = $(".error_1:eq(3)");
        //cardNoObj.html("请输入银行卡号");
        //cardNoObj.removeClass("none");
        //cardNoObj.addClass("show");
        //errorShow(cardNoObj);
        alert("请输入银行卡号");
		return;
	}

    cardNo = cardNo.replace(/\ +/g,"");

    var reg = /^(\d{16}|\d{19})$/;
	if(!reg.test(cardNo)){
        var cardNoObj = $(".error_1:eq(3)");
        //cardNoObj.html("请输入16或者19位的数字");
        //cardNoObj.removeClass("none");
        //cardNoObj.addClass("show");
        //errorShow(cardNoObj);
        alert("请输入16或者19位的数字");
        return;
	}

	if(authCode == ''){
        var authCodeObj = $(".error_1:eq(4)");
        //authCodeObj.html("请输入验证码");
        //authCodeObj.removeClass("none");
        //authCodeObj.addClass("show");
        //errorShow(authCodeObj);
        alert("请输入验证码");
		return;
	}
	//校验手机验证码验证码
    var checkData = {
        'auth_code':authCode
    };
	var checkAuthCode = false;

    $.ajax({
        url : path+"/sinopay/getwayPay.do?action=checkYzm",
        type : "POST",
        async: false,
        datatype:"json",
        data : checkData,
        success : function(data, stats) {
            var result = eval('(' + data + ')');
            if(result.success != 'true'){
                checkAuthCode = true;
			}
        },
        error : function(data) {
            // window.location.href="/yc"  //跳转到错误页面
        }
    });
    if(checkAuthCode){
        var authCodeObj = $(".error_1:eq(4)");
        //authCodeObj.html("验证码错误");
        //authCodeObj.removeClass("none");
        //authCodeObj.addClass("show");
        //errorShow(authCodeObj);
        alert("验证码错误");
    	return;
	}
	var name = '';//姓名
	var $tag_name = $('#name')[0].tagName;
	if($tag_name == 'LABEL'){
		name = $('#name').text();
	} else {
		name = $("#name").val();//姓名
	}

	var cardIDType = '';//证件类型
	var $tag_cardIDType = $('#cardIDType')[0].tagName;
	if($tag_cardIDType == 'LABEL'){
		cardIDType = $('#cardIDType').text();
	} else {
		cardIDType = $("#cardIDType").val();//证件类型
	}

	var cardIdNo = '';//证件号码
	var $tag_cardIdNo = $('#cardIdNo')[0].tagName;
	if($tag_cardIdNo == 'LABEL'){
		cardIdNo = $("#cardIdNo").text();//证件号码
	} else {
		cardIdNo = $("#cardIdNo").val();//证件号码
	}

	var mobile = '';//手机号码
	var $tag_mobile = $('#mobile')[0].tagName;
	if($tag_mobile == 'LABEL'){
		mobile = $("#mobile").text();//手机号码
	} else {
		mobile = $("#mobile").val();//手机号码
	}


	//设置立即提交按钮不可用
	$("#paymentButton").attr({"disabled":"disabled"});
	$("#paymentButton").addClass("no_btn");

	var requestData = {
		'pay_no':_payNo,
		'sign_msg':sign,
		'bank_code':bankCode,
		'card_no':cardNo,
		'name':name,
		'card_id_type':cardIDType,
		'card_id_no':cardIdNo,
		'mobile':mobile,
		'auth_code':authCode,
		'merchant_code':_merchantCode
	};
	$("#activity_pane").showLoading();
	$.ajax({
		url : path+"/sinopay/getwayPay.do?action=confirmPayment",
		type : "POST",
		datatype:"json",
		data : requestData,
		success : function(data, stats) {
			var result = eval('(' + data + ')');
			//alert("回调地址:"+result.syncNotifyUrl);
            window.location.href = result.syncNotifyUrl;
		},
		error : function(data) {
			// window.location.href="/yc"  //跳转到错误页面
		}
	});

}

function errorShow(obj){
    setTimeout(function() {
        obj.removeClass("show");
        obj.addClass("none");
        obj.html("");
    } ,2000);
}


var countdown=60;
var count = 0;
function settime(obj) {
	if (countdown == 0) {
		obj.text("获取验证码");
		countdown = 60;
        count = 0;
		return;
	} else {

		obj.text("重新发送("+countdown+")");

		countdown--;
        count ++;
	}
	setTimeout(function() { settime(obj) } ,1000)
}
function validate(){
//未选择银行

//银行卡号
if($("#cardNo").val()==null){
$(".error_1:eq(0)").removeClass("none");
$(".error_1:eq(0)").addClass("show");
}
// 验证码输入框
    if($("#authCode").val()==null){
        $(".error_1:eq(1)").removeClass("none");
        $(".error_1:eq(1)").addClass("show");
    }
}
//发送验证码
//提示信息： 验证码不正确或已过期，请重新获取
function  sendYzm() {
	//校验页面字段
    if(count > 0){
        return;
	}
    settime($("#getmobliecode"));
	var mobile = '';//手机号码
	var $tag_mobile = $('#mobile')[0].tagName;
	if($tag_mobile == 'LABEL'){
		mobile = $("#mobile").text();//手机号码
	} else {
		mobile = $("#mobile").val();//手机号码
	}
	var requestData = {
		'mobile':mobile
	};

	$.ajax({
		url : path+"/sinopay/getwayPay.do?action=sendYzm",
		type : "POST",
		datatype:"json",
		data : requestData,
		success : function(data, stats) {

		},
		error : function(data) {

		}
	});
}

